﻿#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
//此文件
/*
关于阴阳历：
阳历（公历）：大月31天，小月30天，闰年（365天）二月28天，平年（366天）二月29天；
阴历：大月30天，小月29天，一年354天；
农历：阴阳合历，大月30天，小月29天；不区分闰平年，引入闰月，19年7闰，平均两到三年一闰;
*/

//全局变量
typedef struct _lunar//储存农历相关信息 
{
    int year;
    int month;
    int day;
    int is_leap;
} lunar_t;
lunar_t lunar;
char tgdz[6];  //天干地支
char sx[3];       //生肖
int monthTotal[13] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
//农历节日储存
typedef struct {
    const char* name;
    int lunar_month;  // 农历月
    int lunar_day;    // 农历日
} LunarHoliday;
LunarHoliday lunar_holidays[10] = {
    {"春节", 1, 1},
    {"元宵节", 1, 15},
    {"龙抬头", 2, 2},
    {"清明节" ,4, 5},
    {"端午节", 5, 5},
    {"七夕节", 7, 7},
    {"中秋节", 8, 15},
    {"重阳节", 9, 9},
    {"腊八节", 12, 8},
    {"除夕", 12, 29},
};
//公历节日储存
typedef struct {
    const char* name;
    int gm;  // 公历月
    int gd;  // 公历日
} Holiday;

Holiday holidays[16] = {
    {"元旦", 1, 1},
    {"情人节", 2, 14},
    {"妇女节", 3, 8},
    {"植树节", 3, 12},
    {"愚人节", 4, 1},
    {"劳动节", 5, 1},
    {"青年节", 5, 4},
    {"儿童节", 6, 1},
    {"建党节", 7, 1},
    {"建军节", 8, 1},
    {"胜利日", 9, 3},
    {"教师节", 9, 10},
    {"九一八", 9, 18},
    {"国庆节", 10, 1},
    {"公祭日", 9, 13},
    {"圣诞节", 12, 25}
};
const char* ChDay[] = //农历月中日的叫法 
{ "*","初一","初二","初三","初四","初五",
    "初六","初七","初八","初九","初十",
    "十一","十二","十三","十四","十五",
    "十六","十七","十八","十九","二十",
    "廿一","廿二","廿三","廿四","廿五",
    "廿六","廿七","廿八","廿九","三十" };
const char* ChMonth[] = { "*","正","二","三","四","五","六","七","八","九","十","冬","腊" };//农历月名称 
//农历数据
unsigned int lunar200y[199] =
{
    0x04AE53,0x0A5748,0x5526BD,0x0D2650,0x0D9544,0x46AAB9,0x056A4D,0x09AD42,0x24AEB6,0x04AE4A,/*1901-1910*/
    0x6A4DBE,0x0A4D52,0x0D2546,0x5D52BA,0x0B544E,0x0D6A43,0x296D37,0x095B4B,0x749BC1,0x049754,/*1911-1920*/
    0x0A4B48,0x5B25BC,0x06A550,0x06D445,0x4ADAB8,0x02B64D,0x095742,0x2497B7,0x04974A,0x664B3E,/*1921-1930*/
    0x0D4A51,0x0EA546,0x56D4BA,0x05AD4E,0x02B644,0x393738,0x092E4B,0x7C96BF,0x0C9553,0x0D4A48,/*1931-1940*/
    0x6DA53B,0x0B554F,0x056A45,0x4AADB9,0x025D4D,0x092D42,0x2C95B6,0x0A954A,0x7B4ABD,0x06CA51,/*1941-1950*/
    0x0B5546,0x555ABB,0x04DA4E,0x0A5B43,0x352BB8,0x052B4C,0x8A953F,0x0E9552,0x06AA48,0x6AD53C,/*1951-1960*/
    0x0AB54F,0x04B645,0x4A5739,0x0A574D,0x052642,0x3E9335,0x0D9549,0x75AABE,0x056A51,0x096D46,/*1961-1970*/
    0x54AEBB,0x04AD4F,0x0A4D43,0x4D26B7,0x0D254B,0x8D52BF,0x0B5452,0x0B6A47,0x696D3C,0x095B50,/*1971-1980*/
    0x049B45,0x4A4BB9,0x0A4B4D,0xAB25C2,0x06A554,0x06D449,0x6ADA3D,0x0AB651,0x093746,0x5497BB,/*1981-1990*/
    0x04974F,0x064B44,0x36A537,0x0EA54A,0x86B2BF,0x05AC53,0x0AB647,0x5936BC,0x092E50,0x0C9645,/*1991-2000*/
    0x4D4AB8,0x0D4A4C,0x0DA541,0x25AAB6,0x056A49,0x7AADBD,0x025D52,0x092D47,0x5C95BA,0x0A954E,/*2001-2010*/
    0x0B4A43,0x4B5537,0x0AD54A,0x955ABF,0x04BA53,0x0A5B48,0x652BBC,0x052B50,0x0A9345,0x474AB9,/*2011-2020*/
    0x06AA4C,0x0AD541,0x24DAB6,0x04B64A,0x69573D,0x0A4E51,0x0D2646,0x5E933A,0x0D534D,0x05AA43,/*2021-2030*/
    0x36B537,0x096D4B,0xB4AEBF,0x04AD53,0x0A4D48,0x6D25BC,0x0D254F,0x0D5244,0x5DAA38,0x0B5A4C,/*2031-2040*/
    0x056D41,0x24ADB6,0x049B4A,0x7A4BBE,0x0A4B51,0x0AA546,0x5B52BA,0x06D24E,0x0ADA42,0x355B37,/*2041-2050*/
    0x09374B,0x8497C1,0x049753,0x064B48,0x66A53C,0x0EA54F,0x06B244,0x4AB638,0x0AAE4C,0x092E42,/*2051-2060*/
    0x3C9735,0x0C9649,0x7D4ABD,0x0D4A51,0x0DA545,0x55AABA,0x056A4E,0x0A6D43,0x452EB7,0x052D4B,/*2061-2070*/
    0x8A95BF,0x0A9553,0x0B4A47,0x6B553B,0x0AD54F,0x055A45,0x4A5D38,0x0A5B4C,0x052B42,0x3A93B6,/*2071-2080*/
    0x069349,0x7729BD,0x06AA51,0x0AD546,0x54DABA,0x04B64E,0x0A5743,0x452738,0x0D264A,0x8E933E,/*2081-2090*/
    0x0D5252,0x0DAA47,0x66B53B,0x056D4F,0x04AE45,0x4A4EB9,0x0A4D4C,0x0D1541,0x2D92B5          /*2091-2099*/
};
int check_year(int year) {//检查平年闰年函数

    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
        return 1;//闰年返回1
    }
    else {
        return 0;//平年返回0
    }
}
//检查输入日期正确性(公历)
int examdate(int y, int m, int d) {
    if (((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && (d <= 0 || d > 31)) || m <= 0 || m > 12 || ((m == 4 || m == 6 || m == 9 || m == 11) && (d <= 0 || d > 30))) {
        return 1;
    }if (check_year(y) == 1 && m == 2 && (d <= 0 || d > 29)) {
        return 1;
    }if (check_year(y) == 0 && m == 2 && (d <= 0 || d > 28)) {
        return 1;
    }if (y < 1900 || y>2100) {
        return 1;
    }
    else {
        return 0;
    }
}
// 获取当前系统时间（Windows）
SYSTEMTIME GetCurTime() {
    SYSTEMTIME st;
    GetLocalTime(&st);
    return st;
}
//打印当前时间
void PrintCurrentTime() {
    SYSTEMTIME st = GetCurTime();
    printf("当前时间: %04d-%02d-%02d %02d:%02d\n",
        st.wYear, st.wMonth, st.wDay,
        st.wHour, st.wMinute);
}
void gotoxy(int x, int y)
{
    COORD pos = { x,y };
    HANDLE xy = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(xy, pos);
}
// 判断任意两个公历日期之间的天数
int days(int year1, int month1, int day1, int year2, int month2, int day2) {
    int sum = 0;

    // 计算year1已经度过的天数
    int a = monthTotal[month1 - 1] + day1;
    if (check_year(year1) && month1 > 2) {
        a++;
    }

    // 计算year2已经度过的天数
    int b = monthTotal[month2 - 1] + day2;
    if (check_year(year2) && month2 > 2) {
        b++;
    }

    // 如果是同一年，直接相减
    if (year1 == year2) {
        sum = b - a;
    }
    else {
        // 计算整年的天数
        for (int i = year1 + 1; i < year2; i++) {
            sum += check_year(i) ? 366 : 365;
        }

        // 加上year2的天数，减去year1的天数
        sum += b;
        sum += (check_year(year1) ? 366 : 365) - a;
    }
    return sum;
}
// 计算指定日期是星期几（0=星期日，1=星期一，...，6=星期六）
int day_of_week(int year, int month, int day) {
    if (month < 3) {
        month += 12;
        year--;
    }
    int c = year / 100;
    year %= 100;
    int w = (c / 4 - 2 * c + year + year / 4 + 13 * (month + 1) / 5 + day - 1) % 7;
    return (w + 7) % 7;  // 确保结果为非负数
}
// 获取指定月份的天数
int days_in_month(int year, int month) {
    static int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 && check_year(year))
        return 29;
    return days[month - 1];
}

//公历转农历的相关函数//公历转农历的相关函数//公历转农历的相关函数
int lunarLeapMonth(int y)//判断当前年是否闰月，并得到闰的月份；不闰月就得到0。 
{
    //按位与操作，0xf00000：1111 0000 0000 0000 0000 0000
    return((unsigned int)(lunar200y[y - 1901] & 0xf00000) >> 20);//把储存的前四位数据取出来
}
int lunarMonthDays(int y, int m)//由于农历朔望月30或29是不确定的，此处用来判断输出。看下指定年份在指定月是多少天。 
{
    return((unsigned int)(lunar200y[y - 1901] & (0x80000 >> (m - 1))) ? 30 : 29);/*0x80000：1000 0000 0000 0000 0000；
    经过移位，可以把第m个月存的数据调用出来*/
}
int lunarLeapMonthDays(int y)//如果闰月，得到闰月的大小，是30天还是29天
{
    if (lunarLeapMonth(y))
    {
        return((unsigned int)(lunar200y[y - 1901] & (0x80000 >> (lunarLeapMonth(y) - 1))) ? 30 : 29);/*0x80000：1000 0000 0000 0000 0000；
        0x80000>>(lunarLeapMonth(y)-1)部分，移动1的位置 控制提取闰月在数据中的位置*/
    }
    else
    {
        return 0;
    }
}
int lunarSpringDate(int y, int* solar_m, int* solar_d)/*得到农历的春节，即正月初一在阳历中日期。
在函数中，把数据提取出来，利用指针变量来储存在函数中得到的结果。*/
{
    if (!solar_m || !solar_d)
        return -1;
    *solar_m = ((unsigned int)(lunar200y[y - 1901] & 0x0060)) >> 5;
    *solar_d = lunar200y[y - 1901] & 0x1f;
    return 0;
}
int toLunar(int year, int month, int day, lunar_t* lunar) {
    int bySpring, bySolar, daysPerMonth;
    int index, flag;

    // 获取春节公历日期
    int spring_solar_m, spring_solar_d;
    lunarSpringDate(year, &spring_solar_m, &spring_solar_d);

    // 计算阳历日距离春节的天数
    if (spring_solar_m == 1) {
        bySpring = spring_solar_d - 1;
    }
    else {
        bySpring = spring_solar_d - 1 + 31;
    }

    bySolar = monthTotal[month - 1] + day - 1;
    if (check_year(year) && month > 2) bySolar++;

    if (bySolar >= bySpring) {  // 阳历日在春节后
        bySolar -= bySpring;
        index = 1;
        lunar->month = 1;
        flag = 1;
        daysPerMonth = lunarMonthDays(year, index);
        while (bySolar >= daysPerMonth) {
            bySolar -= daysPerMonth;
            if (lunar->month == lunarLeapMonth(year)) {
                flag = -flag;
                if (flag) lunar->month++;
            }
            else {
                lunar->month++;
            }
            index++;
            daysPerMonth = lunarMonthDays(year, index);
        }
        lunar->day = bySolar + 1;
        lunar->year = year;
    }
    else {  // 阳历日在春节前（属于上一年）
        bySpring -= bySolar;
        lunar->year = year - 1;
        lunar->month = 12;
        index = (lunarLeapMonth(lunar->year) ? 13 : 12);
        flag = 1;
        daysPerMonth = lunarMonthDays(lunar->year, index);
        while (bySpring > daysPerMonth) {
            bySpring -= daysPerMonth;
            index--;
            if (flag) lunar->month--;
            if (lunar->month == lunarLeapMonth(lunar->year)) flag = -flag;
            daysPerMonth = lunarMonthDays(lunar->year, index);
        }
        lunar->day = daysPerMonth - bySpring + 1;
    }

    // ====== 闰月修正逻辑 ======
    int leap_month = lunarLeapMonth(lunar->year);
    lunar->is_leap = 0;
    if (leap_month > 0) {
        if (index == leap_month + 1) {  // 当前为闰月
            lunar->is_leap = 1;
            lunar->month = leap_month;
        }
        else if (index > leap_month) {  // 闰月后的月份索引修正
            lunar->month = index - 1;
        }
    }

    return 0;
}
/*求某年干支生肖部分*/
void gz_sx(int year) //判断干支与生肖。 
{
    int a, b, chazhi;

    char tiangan[10][4] = { "甲","乙","丙","丁","戊","己","庚","辛","壬","癸" };
    char dizhi[12][4] = { "子","丑","寅","卯","辰","巳","午","未","申","酉","戍","亥" };
    char shengxiao[12][4] = { "鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪" };
    chazhi = year - 2020;//2020年农历年主要是庚子年,以其为基准，推算。  
    a = (chazhi + 6) % 10;//庚是定义天干数组中第七个汉字   
    b = (chazhi + 0) % 12;//子是定义地支数组中第一个汉字
    if (a < 0)
        a = a + 10;
    if (b < 0)
        b = b + 12;
    strncpy_s(tgdz, tiangan[a], 4);
    strcat_s(tgdz, dizhi[b]);
    strncpy_s(sx, shengxiao[b], 3);
}
void  nongli(int year, int month, int day)//输入想要判断的公历日期，得到农历
{
    int year1, month1, day1;
    year1 = year;
    month1 = month;
    day1 = day;//后续求春节日期，会改变储存的，year,month,day的值，先储存下，便于后面判断。
    lunarSpringDate(year1, &month1, &day1);//得到春节日期，为了具体干支年的判断 
    toLunar(year, month, day, &lunar);
    // printf("对应农历为 ");
    if (month > month1)//一个公历年有涉及到两个农历年；以春节为界限。之前为上一农历年。之后为下一农历年 
        gz_sx(year);//用到下一个农历年干支 
    else if (month == month1 && day >= day1)
        gz_sx(year);//用到下一个农历年干支
    else gz_sx(year - 1);//用到上一个农历年干支 
}

//农历转公历的相关函数//农历转公历的相关函数//农历转公历的相关函数
//输入参数为农历日期（lyear, lmonth, lday, is_leap），输出参数为公历日期（gyear, gmonth, gday），遍历公历日期，将其转为农历日期后与所给农历日期一一比对
int days_in_month(int year, int month);
int GetGre(int lyear, int lmonth, int lday, int is_leap, int* gyear, int* gmonth, int* gday) {
    // 遍历可能的公历年份（春节可能跨年）
    for (int y = lyear - 1; y <= lyear + 1; y++) {
        for (int m = 1; m <= 12; m++) {
            int days = days_in_month(y, m);
            for (int d = 1; d <= days; d++) {
                lunar_t lunar_date;
                toLunar(y, m, d, &lunar_date);

                // 获取实际闰月信息
                int actual_leap_month = lunarLeapMonth(lyear);

                // 严格匹配条件
                if (lunar_date.year == lyear &&
                    lunar_date.day == lday &&
                    lunar_date.month == lmonth &&
                    (
                        (is_leap && actual_leap_month == lmonth && lunar_date.is_leap) ||  // 闰月匹配
                        (!is_leap && (actual_leap_month != lmonth || !lunar_date.is_leap))  // 非闰月匹配
                        ) ){
                    *gyear = y;
                    *gmonth = m;
                    *gday = d;
                    return 0;
                }
            }
        }
    }
    return -1;
}
//节气的相关函数//节气的相关函数//节气的相关函数

// 新的节气数据结构（仅保留名称）
typedef struct {
    const char* name;   // 节气名称
} SolarTerm;
// 节气名称表（按文档顺序0-23）
SolarTerm terms[] = {
    {"小寒"}, {"大寒"}, {"立春"}, {"雨水"}, {"惊蛰"}, {"春分"},
    {"清明"}, {"谷雨"}, {"立夏"}, {"小满"}, {"芒种"}, {"夏至"},
    {"小暑"}, {"大暑"}, {"立秋"}, {"处暑"}, {"白露"}, {"秋分"},
    {"寒露"}, {"霜降"}, {"立冬"}, {"小雪"}, {"大雪"}, {"冬至"},
};
// 节气计算（1900-2100）
void calculate_solar_terms(int year, int terms_data[24][3]) {
    int y = year - 1900; // y从1900年开始计算

    for (int x = 0; x < 24; x++) {
        // 1.公式计算积日F
        double F = 365.242 * y + 6.2 + 15.22 * x - 1.9 * sin(0.262 * x);
        int total_days = static_cast<int>(F - 0.9); 

        // 2. 将积日转换为公历日期（从1900-01-0即1899-12-31开始）
        int days_remaining = total_days;
        int gy = 1900;
        while (days_remaining >= (check_year(gy) ? 366 : 365)) {
            days_remaining -= (check_year(gy) ? 366 : 365);
            gy++;
        }

        // 计算月份和日
        int gm = 1;
        while (days_remaining >= days_in_month(gy, gm)) {
            days_remaining -= days_in_month(gy, gm);
            gm++;
        }
        int gd = days_remaining + 1; // 积日从0开始

        // 3. 处理跨年（如小寒可能在上一年12月）
        if (gm == 12 && gd > 31) {
            gy++;
            gm = 1;
            gd -= 31;
        }

        // 4. 写入结果
        terms_data[x][0] = gy;
        terms_data[x][1] = gm;
        terms_data[x][2] = gd;
    }
}

char current_solar_term[20] = ""; // 存储节气名称
int days_to_next_solar_term = -1; // 存储距离下一个节气的天数，-1表示未计算

// 判断两个日期是否相同
int is_same_date(int y1, int m1, int d1, int y2, int m2, int d2) {
    return y1 == y2 && m1 == m2 && d1 == d2;
}

// 判断日期1是否早于日期2
int is_date_earlier(int y1, int m1, int d1, int y2, int m2, int d2) {
    if (y1 < y2) return 1;
    if (y1 > y2) return 0;
    if (m1 < m2) return 1;
    if (m1 > m2) return 0;
    return d1 < d2;
}

void check_solar_term(int y, int m, int d) {
    // 初始化全局变量
    strcpy_s(current_solar_term, "");
    days_to_next_solar_term = -1;

    // 检查日期有效性
    if (examdate(y, m, d)) {
        return;
    }

    // 生成当前年的节气数据
    int terms_data[24][3];
    calculate_solar_terms(y, terms_data);

    // 检查当前日期是否为节气
    for (int i = 0; i < 24; i++) {
        int term_year = terms_data[i][0];
        int term_month = terms_data[i][1];
        int term_day = terms_data[i][2];
        if (is_same_date(term_year, term_month, term_day, y, m, d)) {
            strcpy_s(current_solar_term, terms[i].name);
            days_to_next_solar_term = 0;
            return;
        }
    }

    // 寻找下一个最近的节气
    int min_days = 366 * 2; // 初始化为一个大值
    int found = 0;

    // 检查当前年的后续节气
    for (int i = 0; i < 24; i++) {
        int term_year = terms_data[i][0];
        int term_month = terms_data[i][1];
        int term_day = terms_data[i][2];
        if (is_date_earlier(y, m, d, term_year, term_month, term_day)) {
            int diff = days(y, m, d, term_year, term_month, term_day);
            if (diff < min_days) {
                min_days = diff;
                strcpy_s(current_solar_term, terms[i].name);
                days_to_next_solar_term = diff;
                found = 1;
            }
        }
    }
    // 如果当前年未找到，检查下一年的第一个节气（小寒）
    if (!found) {
        if (y + 1 > 2099) return; // 超出范围
        int next_year_terms[24][3];
        calculate_solar_terms(y + 1, next_year_terms);
        int term_year = next_year_terms[0][0];
        int term_month = next_year_terms[0][1];
        int term_day = next_year_terms[0][2];
        days_to_next_solar_term = days(y, m, d, term_year, term_month, term_day);
        strcpy_s(current_solar_term, terms[0].name);
    }
}

//打印主页面
void print_screen() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    // 设置白色背景 + 黑色前景
    SetConsoleTextAttribute(hConsole,
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("超级万年历\n");
    // 设置白色背景 + 红色前景（加粗）
    SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    PrintCurrentTime();
    //菜单
    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_RED |  //前景颜色
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);//白色背景
    printf("选项1：查询农历                \n");
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("选项2：查询公历                \n");
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("选项3：显示月历                \n");
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("选项4：查询某天距今天的天数    \n");
    SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_BLUE |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("选项5：查询距今天相应天数的日期\n");
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("选项6：查询任意两天之间的天数差\n");
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("选项7：显示二十四节气          \n");
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE |
        BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    printf("选项8：显示节日                \n");
    // 恢复默认颜色（通常是灰色文本 + 黑色背景）
    SetConsoleTextAttribute(hConsole, 7);
    printf("请输入您的选项：\n");
}

void print_single_month(int year, int month) { // 打印月历
    printf("\t\t\t%d年%d月\n\n", year, month);
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    printf("日\t");
    SetConsoleTextAttribute(hConsole, 7);
    printf("一\t二\t三\t四\t五\t");
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    printf("六\n\n");
    SetConsoleTextAttribute(hConsole, 7);
    int w = day_of_week(year, month, 1);//该月第一天是星期几
    // 打印前置空格
    for (int i = 0; i < w; i++) {
        printf(" \t");
    }
    int p = w;//记录前置空格数量
    int flag = 1;//是否打印空格
    int day_ = 1;//记录打印农历的天数
    int t = 0;//每行打印了多少天
    // 打印该月的日期
    int days = days_in_month(year, month);
    for (int day = 1; day <= days; day++) {
        //打印公历
        if (day_of_week(year, month, day) == 6 || day_of_week(year, month, day) == 0) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            printf("%d\t", day);
            SetConsoleTextAttribute(hConsole, 7);
        }
        else {
            printf("%d\t", day);
        }
        w = w + 1;
        t = t + 1;
        if (w % 7 == 0) {  // 每7天换行
            printf("\n");
            //打印农历
            if (flag) {//打印前置空格
                for (int i = 0; i < p; i++) {
                    printf(" \t");
                }
                flag = 0;

                for (int i = 0; i < t; i++) {
                    nongli(year, month, day_);
                    check_solar_term(year, month, day_);
                    int flag2 = 0;
                    char l_name[9];
                    for (int i = 0;i < 10; i++) {
                        if (lunar.month == lunar_holidays[i].lunar_month) {
                            if (lunar.day == lunar_holidays[i].lunar_day) {
                                flag2 = 1;
                                strncpy_s(l_name, lunar_holidays[i].name, 9);
                                break;
                            }
                        }
                    }
                    int flag3 = 0;
                    char g_name[15];
                    for (int i = 0;i < 16; i++) {
                        if (month == holidays[i].gm) {
                            if (day_ == holidays[i].gd) {
                                flag3 = 1;
                                strncpy_s(g_name, holidays[i].name, 15);
                                break;
                            }
                        }
                    }
                    //优先显示传统节日
                    if (flag2)
                    {
                        printf("%s\t", l_name);
                    }
                    //节气
                    else if (days_to_next_solar_term == 0)
                    {
                        printf("%s  \t", current_solar_term);
                    }
                    //公历节日
                    else if (flag3) {
                        printf("%s\t", g_name);
                    }
                    else if (ChDay[lunar.day] == "初一") {
                        if (lunar.is_leap == 1) { //对闰月的处理 
                            printf("闰%s月\t", ChMonth[lunar.month]);
                        }
                        else {
                            printf("%s月  \t", ChMonth[lunar.month]);
                        }
                    }
                    else {
                        printf("%s  \t", ChDay[lunar.day]);
                    }
                    day_++;
                }
                printf("\n");
                t = 0;
            }

            else {
                for (int i = 0; i < t; i++) {
                    nongli(year, month, day_);
                    check_solar_term(year, month, day_);
                    int flag2 = 0;
                    char l_name[9];
                    for (int i = 0;i < 10; i++) {
                        if (lunar.month == lunar_holidays[i].lunar_month) {
                            if (lunar.day == lunar_holidays[i].lunar_day) {
                                flag2 = 1;
                                strncpy_s(l_name, lunar_holidays[i].name, 9);
                                break;
                            }
                        }
                    }
                    int flag3 = 0;
                    char g_name[15];
                    for (int i = 0;i < 16; i++) {
                        if (month == holidays[i].gm) {
                            if (day_ == holidays[i].gd) {
                                flag3 = 1;
                                strncpy_s(g_name, holidays[i].name, 15);
                                break;
                            }
                        }
                    }
                    //优先显示传统节日
                    if (flag2)
                    {
                        printf("%s\t", l_name);
                    }
                    //节气
                    else if (days_to_next_solar_term == 0)
                    {
                        printf("%s  \t", current_solar_term);
                    }
                    //公历节日
                    else if (flag3) {
                        printf("%s\t", g_name);
                    }
                    else if (ChDay[lunar.day] == "初一") {
                        if (lunar.is_leap == 1) { //对闰月的处理 
                            printf("闰%s月\t", ChMonth[lunar.month]);
                        }
                        else {
                            printf("%s月  \t", ChMonth[lunar.month]);
                        }
                    }
                    else {
                        printf("%s  \t", ChDay[lunar.day]);
                    }
                    day_++;
                }
                printf("\n");
                t = 0;
            }
            printf("\n");
        }
    }
    if (day_ - 1 != days) {
        printf("\n");
        for (; day_ <= days; day_++)
        {
            nongli(year, month, day_);
            if (ChDay[lunar.day] == "初一") {
                if (lunar.is_leap == 1) { //对闰月的处理 
                    printf("闰%s月\t", ChMonth[lunar.month]);
                }
                else {
                    printf("%s月\t", ChMonth[lunar.month]);
                }
            }
            else {
                printf("%s\t", ChDay[lunar.day]);
            }
        }
    }
    printf("\n"); // 确保最后有换行符
}
int lunar_calendar() {//选项一查询农历
    int x;
    do {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        system("cls");
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN  );
           
        printf("当前界面：查询农历\n");
        printf("请按格式输入想要查询的公历日期:(例如2020年7月1日，输入2020-7-1) \n注意：范围为1901-2-19到2099-12-31\n");
        int year, month, day;
        scanf_s("%d-%d-%d", &year, &month, &day);
        if (examdate(year, month, day) == 1) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY );
            printf("输入格式错误！若要重新查询请输入888  , 返回主界面请输入999，退出程序请输入666：");
            scanf_s("%d", &x);
            if (x == 999) return 0;
            if (x == 666) return 1;
        }
        else {
            print_single_month(year, month);
            nongli(year, month, day);

            // 检查当前日期的节气情况
            check_solar_term(year, month, day);
            // 输出结果
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
            gotoxy(70, 6);
            printf("★ 日期便签");
            SetConsoleTextAttribute(hConsole, 7);
            gotoxy(70, 7);
            printf("当前日期: %d年%d月%d日  ", year, month, day);
            for (int i = 0; i < 10; i++) {
                if (lunar.month == lunar_holidays[i].lunar_month) {
                    if (lunar.day == lunar_holidays[i].lunar_day) {
                        gotoxy(96, 7);
                        SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                        printf("%s", lunar_holidays[i].name);
                        SetConsoleTextAttribute(hConsole, 7);
                    }
                }
            }
            gotoxy(70, 9);
            printf("%d年是", year);
            SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            gotoxy(75, 9);
            printf("%s%s年 \n\n", tgdz, sx);
            SetConsoleTextAttribute(hConsole, 7);

            if (days_to_next_solar_term == 0) {
                gotoxy(70, 11);
                printf("今天是节气：");
                gotoxy(82, 11);
                SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                printf("%s\n", current_solar_term);
                SetConsoleTextAttribute(hConsole, 7);
            }
            else {
                gotoxy(70, 11);
                printf("下一个节气：");
                gotoxy(82, 11);
                SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                printf("%s\n", current_solar_term);
                SetConsoleTextAttribute(hConsole, 7);
                gotoxy(70, 12);
                printf("距离");
                gotoxy(75, 12);
                SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                printf("%s", current_solar_term);
                SetConsoleTextAttribute(hConsole, 7);
                gotoxy(80, 12);
                printf("还有");
                gotoxy(85, 12);
                SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                printf("%d", days_to_next_solar_term);
                SetConsoleTextAttribute(hConsole, 7);
                gotoxy(88, 12);
                printf("天");
            }
            printf("\n");
            gotoxy(1, 23);
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
               
            printf("是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
            scanf_s("%d", &x);
            if (x == 999) return 0;
            if (x == 666) return 1;
        } 
    } while (x ==888);
    return 0;
}
int calendar() {//选项二查询公历
    int x;
    do {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        system("cls");
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
        printf("当前界面：查询公历\n");
        printf("请输入农历日期（年-月-日 是否闰月，例如：2021-11-18 0）：\n");
        int lyear, lmonth, lday, is_leap;
        scanf_s("%d-%d-%d %d", &lyear, &lmonth, &lday, &is_leap);
        int leap_month = lunarLeapMonth(lyear);
        int max_day = (is_leap) ? lunarLeapMonthDays(lyear) : lunarMonthDays(lyear, lmonth);
        int gyear, gmonth, gday;
        // 输入有效性验证
       
        if (examdate(lyear, lmonth, lday) == 1) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
            printf("输入格式错误！若要重新查询请输入888  , 返回主界面请输入999，退出程序请输入666：");
            scanf_s("%d", &x);

        }
        // 检查闰月是否存在
        
        else if (is_leap && leap_month != lmonth) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
            printf("错误：该年不存在闰%d月！若要重新查询请输入888 ，返回主界面请输入999，退出程序请输入666 ：\n", lmonth);
            scanf_s("%d", &x);
            
        }
        // 检查日期有效性
        
        else if (lday < 1 || lday > max_day) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
               
            printf("错误：农历日期无效！若要重新查询请输入888 ，返回主界面请输入999，退出程序请输入666 ：\n");
            scanf_s("%d", &x);
            
        }
       
        // 调用函数查询公历日期
       
        else {
            if (GetGre(lyear, lmonth, lday, is_leap, &gyear, &gmonth, &gday) == 0) {
                SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
                printf("对应的公历日期为：%04d-%02d-%02d\n", gyear, gmonth, gday);
                SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
                  
                printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                scanf_s("%d", &x);
                
            }
            else {
                SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
                   
                printf("未找到对应的公历日期，请检查输入！重新查询请输入888，返回主界面请输入999，退出程序请输入666:\n");
                scanf_s("%d", &x);
                
            }
        }
        if (x == 999) return 0;
        if (x == 666) return 1;
        
    } while (x == 888);
    return 0;
}
int month() {//选项三显示月历
    int x;
    do {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        system("cls");
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
        printf("当前界面：显示月历\n");
        printf("请输入年份与月份(示例 ：20005 11)\n");
        int year, month;
        scanf_s("%d%d", &year, &month);
        print_single_month(year, month);
        printf("\n");
        gotoxy(1, 23);
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
        printf("是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
        scanf_s("%d", &x);
        if (x == 999) return 0;
        if (x == 666) return 1;
    } while (x == 888);
    return 0;
}
int days_to_today() {//选项四查询某天距今天的天数
    int x;
    do {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        system("cls");
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
        printf("当前界面：查询某天距今天的天数\n");
        int year1, month1, day1, year2, month2, day2;
        SYSTEMTIME st = GetCurTime();
        year1 = st.wYear;
        month1 = st.wMonth;
        day1 = st.wDay;
        SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY );
        printf("当前时间: %04d-%02d-%02d \n", st.wYear, st.wMonth, st.wDay);
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
        printf("请输入要查询的日期（格式：年-月-日 ）：\n");
        scanf_s("%d-%d-%d", &year2, &month2, &day2);
        if (examdate(year2, month2, day2) == 1) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY );
            printf("输入数据错误！若要重新查询请输入888 , 返回主界面请输入999，退出程序请输入666 ：");
            scanf_s("%d", &x);
        }
        else {
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
            if (year2 < year1) {
                int a = days(year2, month2, day2, year1, month1, day1);
                printf("%d-%d-%d 比今天（%d-%d-%d）早 %d 天", year2, month2, day2, year1, month1, day1, a);
                SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
                printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                scanf_s("%d", &x);
            }if (year1 == year2) {
                if (month2 < month1) {
                    int a = days(year2, month2, day2, year1, month1, day1);
                    printf("%d-%d-%d 比今天（%d-%d-%d）早 %d 天", year2, month2, day2, year1, month1, day1, a);
                    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
                    printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                    scanf_s("%d", &x);
                }if (month1 == month2) {
                    if (day2 < day1) {
                        int a = days(year2, month2, day2, year1, month1, day1);
                        printf("%d-%d-%d 比今天（%d-%d-%d）早 %d 天", year2, month2, day2, year1, month1, day1, a);
                        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
                        printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                        scanf_s("%d", &x);
                    }if (day1 == day2) {
                        printf("是同一天");
                        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
                        printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                        scanf_s("%d", &x);
                    }if (day1 < day2) {
                        int a = days(year1, month1, day1, year2, month2, day2);
                        printf("%d-%d-%d 比今天（%d-%d-%d）晚 %d 天", year2, month2, day2, year1, month1, day1, a);
                        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
                        printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                        scanf_s("%d", &x);
                    }
                }if (month1 < month2) {
                    int a = days(year1, month1, day1, year2, month2, day2);
                    printf("%d-%d-%d 比今天（%d-%d-%d）晚 %d 天", year2, month2, day2, year1, month1, day1, a);
                    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
                    printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                    scanf_s("%d", &x);
                }
            }if (year1 < year2) {
                int a = days(year1, month1, day1, year2, month2, day2);
                printf("%d-%d-%d 比今天（%d-%d-%d）晚 %d 天", year2, month2, day2, year1, month1, day1, a);
                SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
                printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
                scanf_s("%d", &x);
            }
        }
        if (x == 999) return 0;
        if (x == 666) return 1;
    } while (x == 888);
    return 0;
}
int date() {//选项五查询距今天相应天数的日期
    int x;
    do {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        system("cls");
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
        printf("当前界面：查询距今天相应天数的日期\n");
        int year, month, day, n;
        SYSTEMTIME st = GetCurTime();
        year = st.wYear;
        month = st.wMonth;
        day = st.wDay; SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY );
        printf("今天日期%d-%d-%d\n", year, month, day);
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
        printf("请输入天数n：\n");
        scanf_s("%d", &n);
        if (examdate(year, month, day)) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY );
            printf("输入数据错误！若要重新查询请输入888 , 返回主界面请输入999，退出程序请输入666 ：");
            scanf_s("%d", &x);
        }
        else {
            //日期转总天数
            auto date_to_days = [](int y, int m, int d) -> long long {
                long long days = d;
                for (int i = 1; i < y; i++)
                    days += check_year(i) ? 366 : 365; // 累加整年天数
                for (int i = 1; i < m; i++)
                    days += days_in_month(y, i);       // 累加整月天数
                return days;
                };

            // 总天数转日期
            auto days_to_date = [](long long total_days, int* y, int* m, int* d) {
                *y = 1;
                // 计算年份
                while (total_days > (check_year(*y) ? 366 : 365)) {
                    total_days -= check_year(*y) ? 366 : 365;
                    (*y)++;
                }
                // 计算月份
                *m = 1;
                while (total_days >= days_in_month(*y, *m)) {
                    total_days -= days_in_month(*y, *m);
                    (*m)++;
                }
                // 计算日
                *d = total_days;//
                };

            // 计算基准日期的总天数
            long long base_days = date_to_days(year, month, day);

            // 计算后n天
            int y_after, m_after, d_after;
            days_to_date(base_days + n, &y_after, &m_after, &d_after);
            int x1 = day_of_week(y_after, m_after, d_after);
            // 计算前n天
            int y_before, m_before, d_before;
            days_to_date(base_days - n, &y_before, &m_before, &d_before);
            int x2 = day_of_week(y_before, m_before, d_before);
            const char* week[] = { "日", "一", "二", "三", "四", "五", "六" };
            // 输出结果（格式化为两位数字）
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
            printf("后%d天：%d-%02d-%02d 星期%s\n", n, y_after, m_after, d_after, week[x1]);
            printf("前%d天：%d-%02d-%02d 星期%s\n", n, y_before, m_before, d_before, week[x2]);
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
            printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
            scanf_s("%d", &x);
        }
       
       
        if (x == 999) return 0;
        if (x == 666) return 1;
    } while (x == 888);
    return 0;
}
int days_between() {//选项六查询任意两天之间的天数差
    int x;
    do {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        system("cls");
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
        printf("当前界面：查询任意两天之间的天数差\n");
        int year1, month1, day1, year2, month2, day2;
        printf("请输入两个日期（格式：年-月-日1 年-月-日2 ）：\n");
        scanf_s("%d-%d-%d %d-%d-%d", &year1, &month1, &day1, &year2, &month2, &day2);
        if ((examdate(year1, month1, day1) == 1) || (examdate(year2, month2, day2) == 1)) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
            printf("输入数据错误！若要重新查询公历请输入888  , 返回主界面请输入999，退出程序请输入666：");
            scanf_s("%d", &x);
        }
        else {
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
            int a = days(year1, month1, day1, year2, month2, day2);
            printf("两天之间的天数差为:%d天", a);
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
            printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
            scanf_s("%d", &x);
        }
     
        if (x == 999) return 0;
        if (x == 666) return 1;
    } while (x == 888);
    return 0;
}
int print_solar_terms() {//选项七显示节气
    int x;
    do {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        system("cls");
        SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
        printf("当前界面：显示节气\n");
        int year;
        printf("请输入要查询的年份（1900-2100）:\n ");
        scanf_s("%d", &year);

        if (year < 1900 || year > 2100) {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
            printf("年份超出范围！若要重新查询请输入888  , 返回主界面请输入999，退出程序请输入666: \n");
            scanf_s("%d", &x);
        }
        else {
            int terms_data[24][3]; // 存储节气年月日
            calculate_solar_terms(year, terms_data);

            printf("%d年节气日期：\n", year);
            for (int i = 0; i < 24; i++) {
                SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE);
                printf("%-6s: %04d-%02d-%02d\t",
                    terms[i].name,
                    terms_data[i][0],
                    terms_data[i][1],
                    terms_data[i][2]);
                if ((i + 1) % 2 == 0)
                {
                    printf("\n");
                }

            }
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED );
            printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
            scanf_s("%d", &x);
        }
        if (x == 999) return 0;
        if (x == 666) return 1;
    } while (x == 888);
    return 0;
}
int print_celebrations() {//选项八显示节日
    int x;
    do {
        system("cls");
        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(handle, FOREGROUND_GREEN);
        printf("当前界面：显示节日\n");
        printf("选项1：显示公历节日\n选项2：显示农历节日\n");
        int b;
        scanf_s("%d", &b);
        if (b == 1) {
            printf("请选择节日所在月份：");
            int m;
            scanf_s("%d", &m);
            SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_BLUE);
            switch (m) {
            case 1:                                                     //标准化输出打表即可 
                printf("\n\t\t 1月1日 元旦(New Year's Day)\n");
                printf("\t\t 1月最后一个星期日 国际麻风节\n\n");
                break;
            case 2:
                printf("\n\t\t 2月2日  世界湿地日(World Wetlands Day)\n");
                printf("\t\t 2月14日 情人节(Valentine's Day)\n\n");
                break;
            case 3:
                printf("\n\t\t 3月3日  全国爱耳日\n");
                printf("\t\t 3月5日  青年志愿者服务日\n");
                printf("\t\t 3月8日  国际妇女节(International Women' Day)\n");
                printf("\t\t 3月9日  保护母亲河日\n");
                printf("\t\t 3月12日 中国植树节(China Arbor Day)\n");
                printf("\t\t 3月14日 白色情人节(White Day)\n");
                printf("\t\t 3月14日 国际警察日(International Policemen' Day)\n");
                printf("\t\t 3月15日 世界消费者权益日(World Consumer Right Day)\n");
                printf("\t\t 3月21日 世界森林日(World Forest Day)\n");
                printf("\t\t 3月21日 世界睡眠日(World Sleep Day)\n");
                printf("\t\t 3月22日 世界水日(World Water Day)\n");
                printf("\t\t 3月23日 世界气象日(World Meteorological Day)\n");
                printf("\t\t 3月24日 世界防治结核病日(World Tuberculosis Day)\n");
                printf("\t\t 3月最后一个完整周的星期一 中小学生安全教育日\n\n");
                break;
            case 4:
                printf("\n\t\t 4月1日  愚人节(April Fools' Day)\n");
                printf("\t\t 4月5日  清明节(Tomb-sweeping Day)\n");
                printf("\t\t 4月7日  世界卫生日(World Health Day)\n");
                printf("\t\t 4月22日 世界地球日(World Earth Day)\n");
                printf("\t\t 4月26日 世界知识产权日(World Intellectual PropertyDay)\n\n");
                break;
            case 5:
                printf("\n\t\t 5月1日  国际劳动节(International Labour Day)\n");
                printf("\t\t 5月3日  世界哮喘日(World Asthma Day)\n");
                printf("\t\t 5月4日  中国青年节(Chinese Youth Day)\n");
                printf("\t\t 5月8日  世界红十字日(World Red-Cross Day)\n");
                printf("\t\t 5月12日 国际护士节(International Nurse Day)\n");
                printf("\t\t 5月15日 国际家庭日(International Family Day)\n");
                printf("\t\t 5月17日 世界电信日(World Telecommunications Day)\n");
                printf("\t\t 5月20日 全国学生营养日\n");
                printf("\t\t 5月23日 国际牛奶日(International Milk Day)\n");
                printf("\t\t 5月31日 世界无烟日(World No-Smoking Day)\n");
                printf("\t\t 5月第二个星期日 母亲节(Mother's Day)\n");
                printf("\t\t 5月第三个星期日 全国助残日\n\n");
                break;
            case 6:
                printf("\n\t\t 6月1日  国际儿童节(International Children's Day)\n");
                printf("\t\t 6月5日  世界环境日(International Environment Day)\n");
                printf("\t\t 6月6日  全国爱眼日\n");
                printf("\t\t 6月17日 世界防治荒漠化和干旱日(World Day to combat desertification)\n");
                printf("\t\t 6月23日 国际奥林匹克日(International Olympic Day)\n");
                printf("\t\t 6月25日 全国土地日\n");
                printf("\t\t 6月26日 国际禁毒日(International Day Against Drug Abuse and Illicit Trafficking)\n");
                printf("\t\t 6月第三个星期日 父亲节(Father's Day)\n\n");
                break;
            case 7:
                printf("\n\t\t 7月1日  中国共产党诞生日(Anniversary of the Founding of the Chinese Communist Party)\n");
                printf("\t\t 7月1日  国际建筑日(International Architecture Day)\n");
                printf("\t\t 7月7日  中国人民抗日战争纪念日\n");
                printf("\t\t 7月11日 世界人口日(World Population Day)\n\n");
                break;
            case 8:
                printf("\n\t\t 8月1日  中国人民解放军建军节(Army Day)\n");
                printf("\t\t 8月12日 国际青年节(International Youth Day)\n\n");
                break;
            case 9:
                printf("\n\t\t 9月8日  国际扫盲日(International Anti-illiteracy Day)\n");
                printf("\t\t 9月10日 中国教师节(Teacher's Day)\n");
                printf("\t\t 9月16日 中国脑健康日\n");
                printf("\t\t 9月16日 国际臭氧层保护日(International Day for the Preservation of the Ozone Layer)\n");
                printf("\t\t 9月20日 全国爱牙日\n");
                printf("\t\t 9月21日 世界停火日(World Cease-fire Day)\n");
                printf("\t\t 9月27日 世界旅游日(World Tourism Day)\n");
                printf("\t\t 9月第三个星期二 国际和平日(International Peace Day)\n");
                printf("\t\t 9月第三个星期六 全国国防教育日\n");
                printf("\t\t 9月第四个星期日 国际聋人节(International Day of the Deaf)\n\n");
                break;
            case 10:
                printf("\n\t\t 10月1日  中华人民共和国国庆节(National Day)\n");
                printf("\t\t 10月1日  国际老年人日(International Day of Older Persons)\n");
                printf("\t\t 10月4日  世界动物日(World Animal Day)\n");
                printf("\t\t 10月5日  世界教师日(World Teachers' Day)（联合国教科文组织确立）\n");
                printf("\t\t 10月9日  世界邮政日(World Post Day)\n");
                printf("\t\t 10月15日 国际盲人节(International Day of the Blind)\n");
                printf("\t\t 10月15日 世界农村妇女日(World Rural Women's Day)\n");
                printf("\t\t 10月16日 世界粮食日(World Food Day)\n");
                printf("\t\t 10月17日 国际消除贫困日(International Day for the Eradication of Poverty)\n");
                printf("\t\t 10月24日 联合国日(United Nations Day)\n");
                printf("\t\t 10月29日 国际生物多样性日(International Biodiversity Day)\n");
                printf("\t\t 10月31日 万圣节(Halloween)\n");
                printf("\t\t 10月的第一个星期一 世界住房日(World Habitat Day)\n");
                printf("\t\t 10月的第二个星斯一 加拿大感恩节(Thanksgiving Day)\n");
                printf("\t\t 10月第二个星期三   国际减轻自然灾害日(International Day for Natural Disaster Reduction)\n");
                printf("\t\t 10月第二个星期四   世界爱眼日(World Sight Day)\n\n");
                break;
            case 11:
                printf("\n\t\t 11月8日  中国记者节\n");
                printf("\t\t 11月9日  消防宣传日\n");
                printf("\t\t 11月14日 世界糖尿病日(World Diabetes Day)\n");
                printf("\t\t 11月17日 国际大学生节\n");
                printf("\t\t 11月25日 国际消除对妇女的暴力日(International Day For the elimination of Violence against Women)\n");
                printf("\t\t 11月最后一个星期四美国感恩节(Thanksgiving Day)\n\n");
                break;
            case 12:
                printf("\n\t\t 12月1日  世界爱滋病日(World AIDS Day)\n");
                printf("\t\t 12月3日  世界残疾人日(World Disabled Day)\n");
                printf("\t\t 12月4日  全国法制宣传日\n");
                printf("\t\t 12月9日  世界足球日(World Football Day)\n");
                printf("\t\t 12月25日 圣诞节(Christmas Day)\n");
                printf("\t\t 12月29日 国际生物多样性日(International Biological Diversity Day)\n\n");
                break;

            }
            SetConsoleTextAttribute(handle, 7);
            SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_RED);
            printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
            scanf_s("%d", &x);
        }
        else if (b == 2) {
            for (int i = 0; i < 10; i++) {
                printf("%s月%s：%s\n", ChMonth[lunar_holidays[i].lunar_month],
                    ChDay[lunar_holidays[i].lunar_day], lunar_holidays[i].name);
            }
            SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_RED);
            printf("\n是否继续查询？继续请按888，返回主界面请输入999，退出程序请输入666:\n");
            scanf_s("%d", &x);
        }
        else {
            SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_INTENSITY );
            printf("选项输入错误 , 若要重新查询请输入888 , 返回主界面请输入999，退出程序请输入666 :");
            scanf_s("%d", &x);
        }
        
       
        if (x == 999) return 0;
        if (x == 666) return 1;
    } while (x == 888);
    return 0;
}
int main() {
    while (1) {
        system("cls");
        print_screen();
        int n;
        scanf_s("%d", &n);
        int should_exit = 0;
        switch (n) {
        case 1: should_exit = lunar_calendar(); break;
        case 2: should_exit = calendar(); break;
        case 3: should_exit = month(); break;
        case 4: should_exit = days_to_today(); break;
        case 5: should_exit = date(); break;
        case 6: should_exit = days_between(); break;
        case 7: should_exit = print_solar_terms(); break;
        case 8: should_exit = print_celebrations(); break;
        }
        if (should_exit) {
            break; // 退出主循环
        }
    }
    system("cls");
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_BLUE );
     printf("\n\n\n\n\n\n\n\n\n\n");
     printf("\t\t\t\t\t\t   _   _      _       _                     \n");
     printf("\t\t\t\t\t\t  | | | |    | |     | |                    \n");
     printf("\t\t\t\t\t\t  | |_| | ___| | ___ | |___      _____  _ __ \n");
     printf("\t\t\t\t\t\t  |  _  |/ _ \\ |/ _ \\ __\\ \\ /\\ / / _ \\| '__|\n");
     printf("\t\t\t\t\t\t  | | | |  __/ |  __/ |_ \\ V  V / (_) | |   \n");
     printf("\t\t\t\t\t\t  |_| |_|\\___|_|\\___|\\__| \\_/\\_/ \\___/|_|   \n");
     printf("\n\n\t\t\t\t\t\t\t\t感谢您的使用！！");
     printf("\n\n\n\n\n\n\n\n");
    return 0;
}